﻿using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Framework
{
    public static class BlockProtoDict
    {
        static Dictionary<BlockType, BlockProto> type2proto = new();
        static Dictionary<string, BlockType> name2type = new();
        static Dictionary<BlockType, string> type2name = new();

        static public BlockProto GetProto(BlockType type)
        {
            return type2proto[type];
        }

        static public void RegeistProto(BlockType type,string name,BlockProto blockProto)
        {
            TryChangeValueOrAdd(type2proto, type, blockProto);
            TryChangeValueOrAdd(name2type, name, type);
            TryChangeValueOrAdd(type2name, type, name);
            Debug.Log($"Regeist block proto {name}");
        }

        private static void TryChangeValueOrAdd<T,U>(Dictionary<T,U> dic,T key,U val)
        {
            if (dic.TryGetValue(key, out U valD))
            {
                valD = val;
                Debug.LogWarning($"Rewrite {key.ToString()}");
            }
            else
            {
                dic.Add(key, val);
            }
        }
    }
}